//
//  choseTakePhotoVIew.m
//  BigJoy
//
//  Created by Rowling on 2017/6/21.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "choseTakePhotoVIew.h"

@interface choseTakePhotoVIew()
@property (nonatomic,strong)NSArray <NSString *>* titleArray;
@property (nonatomic,strong) UIView *selectView;
@property (nonatomic,strong) NSMutableArray *buttonArray;
@end

@implementation choseTakePhotoVIew

-(NSMutableArray *)buttonArray {
    if (_buttonArray == nil) {
        _buttonArray = [[NSMutableArray alloc]init];
    }
    return _buttonArray;
}

-(instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray<NSString *> *)titleArray {
    if (self = [super initWithFrame:frame]) {
        self.titleArray = titleArray;
        [self setUI];
    }
    return self;
}

-(instancetype)initHeadWithFrame:(CGRect)frame titleArray:(NSArray<NSString *> *)titleArray {
    if (self = [super initWithFrame:frame]) {
        self.titleArray = titleArray;
        [self setHeadUI:-1];
    }
    return self;
}

-(instancetype)initHeadWithFrame:(CGRect)frame viewY:(CGFloat)Y titleArray:(NSArray<NSString *> *)titleArray {
    if (self = [super initWithFrame:frame]) {
        self.titleArray = titleArray;
        [self setHeadUI:Y];
    }
    return self;
}

-(RACSubject *)subject {
    if (_subject == nil) {
        _subject = [[RACSubject alloc]init];
    }
    return _subject;
}

-(void)setUI {
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    UIView * selectView = [[UIView alloc]init];
    self.selectView = selectView;
    selectView.frame = CGRectMake(0, self.frame.size.height - 50 * self.titleArray.count, SCREEN_WIDTH, 50 * self.titleArray.count);
    for (int i = 0;i < self.titleArray.count;i++) {
        UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, i * 50, SCREEN_WIDTH, 50)];
        [selectView addSubview:btn];
        UIView * lineView = [[UIView alloc]init];
        lineView.backgroundColor = defaultBackColor;
        [btn addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(btn);
            make.height.equalTo(@1);
        }];
        [btn setTitle:self.titleArray[i] forState:UIControlStateNormal];
        btn.tag = 100 + i;
        [btn setBackgroundColor:[UIColor whiteColor]];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        @weakify(self);
        [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [self.subject sendNext:@(btn.tag - 100)];
            [self removeFromSuperview];
        }];
        [self.buttonArray addObject:btn];
    }
    [self addSubview:selectView];
}

-(void)setHeadUI:(CGFloat)Y {
    self.backgroundColor = [UIColor clearColor];
    UIView * selectView = [[UIView alloc]init];
    self.selectView = selectView;
    selectView.frame = CGRectMake(0, Y >= 0 ? Y : 64, SCREEN_WIDTH, 50 * self.titleArray.count);
    for (int i = 0;i < self.titleArray.count;i++) {
        UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, i * 50, SCREEN_WIDTH, 50)];
        [selectView addSubview:btn];
        UIView * lineView = [[UIView alloc]init];
        lineView.backgroundColor = defaultBackColor;
        [btn addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(btn);
            make.height.equalTo(@1);
        }];
        [btn setTitleColor: [UIColor colorForHex:@"666666"] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setTitle:self.titleArray[i] forState:UIControlStateNormal];
        btn.tag = 100 + i;
        [btn setBackgroundColor:[UIColor whiteColor]];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        @weakify(self);
        [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [self.subject sendNext:@(btn.tag - 100)];
            [self removeFromSuperview];
        }];
        [self.buttonArray addObject:btn];
    }
    [self addSubview:selectView];
}

// 修改字体
-(void)setFont:(UIFont *)font {
    _font = font;
    for (UIButton * btn in self.buttonArray) {
        btn.titleLabel.font = font;
    }
}

-(void)setFontColor:(UIColor *)fontColor {
    _fontColor = fontColor;
    for (UIButton * btn in self.buttonArray) {
        [btn setTitleColor:fontColor forState:UIControlStateNormal];
    }
}

-(void)doAnimation {
    CGFloat y = self.selectView.y;
    self.selectView.y = SCREEN_HEIGHT;
    [UIView animateWithDuration:0.5 animations:^{
        self.selectView.y = y;
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self removeFromSuperview];
}

-(void)dealloc {
    NSLog(@"choseViewDealloc");
}

@end
